package com.emit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.emit.dao.MenuMapper;
import com.emit.domain.Menu;
import com.emit.domain.MenuExample;
import com.emit.form.MenuForm;
import com.emit.service.MenuService;
import com.emit.utils.MenuTreeBuilder;
import com.emit.utils.MenuTreeUtil;
import com.emit.vo.MenuVO;
import com.github.pagehelper.PageSerializable;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单逻辑处理
 *
 * @author zhangchao
 * @version 1.0.0
 * @date 2021-10-28
 */
@Service("menuService")
public class MenuServiceImpl implements MenuService {

    @Resource
    MenuMapper menuMapper;

    /**
     * 根据用户ID查询所有菜单
     *
     * @param id 用户ID
     * @return 所有菜单数据
     */
    @Override
    public List<MenuVO> findUserMenusByUserId(Long id) {
        MenuExample menuExample = new MenuExample();
        menuExample.setOrderByClause("MENU_SEQ");
        List<Menu> userMenus = menuMapper.selectByExample(menuExample);
        //list=》树
        if (userMenus != null && userMenus.size() > 0) {
            List<MenuVO> userMenuVos = userMenus.stream().map(this::menuToMenuVO).collect(Collectors.toList());
            List<MenuVO> rootList = userMenuVos.stream().filter(menu -> new Long(0L).equals(menu.getParentId())).collect(Collectors.toList());
            MenuTreeUtil utils = new MenuTreeUtil(rootList, userMenuVos);
            return utils.getTree();
        }
        return null;
    }

    /**
     * menu转换为MenuVO
     * @param menu 数据库中对应菜单数据
     * @return 菜单页面数据
     */
    private MenuVO menuToMenuVO(Menu menu) {
        MenuVO menuVO = new MenuVO();
        BeanUtil.copyProperties(menu, menuVO);
        return menuVO;
    }

    /**
     * 分页查询所有菜单
     *
     * @param menuForm 菜单表单数据
     * @return 分页数据
     */
    @Override
    public PageSerializable<MenuVO> findMenus(MenuForm menuForm) {
        MenuExample menuExample = new MenuExample();
        MenuExample.Criteria criteria = menuExample.createCriteria();
        if(menuForm.getParentId()!=null){
            criteria.andParentIdEqualTo(menuForm.getParentId());
        }
        if(StringUtils.isNotEmpty(menuForm.getName())){
            criteria.andNameLike(menuForm.getName());
        }
        menuExample.setOrderByClause("MENU_SEQ");
        List<Menu> res = menuMapper.selectByExample(menuExample);
        List<MenuVO> menuVos = MenuTreeBuilder.buildByRecursive(res.stream().map(this::menuToMenuVO).collect(Collectors.toList()));
        return new PageSerializable<>(menuVos);
    }

    /**
     * 菜单下拉框
     * @return 菜单集合数据
     */
    @Override
    public List<Menu> findMenuOptions() {
        MenuExample menuExample = new MenuExample();
        menuExample.setOrderByClause("TYPE ASC");
        return menuMapper.selectByExample(menuExample);
    }

    /**
     * 添加菜单
     * @param menuForm 菜单表单数据
     * @return 添加结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addMenu(MenuForm menuForm) {
        Menu menu = new Menu();
        BeanUtil.copyProperties(menuForm,menu);
        return menuMapper.insert(menuToMenuVO(menu)) > 0;
    }

    /**
     * 查询所有菜单信息
     */
    @Override
    public List<Menu> findAllMenus() {
        MenuExample menuExample = new MenuExample();
        menuExample.setOrderByClause("MENU_SEQ");
        return menuMapper.selectByExample(menuExample);
    }


    /**
     * 同一菜单下名称重复性校验
     */
    @Override
    public Menu checkExistMenu(Menu menu) {
        MenuExample menuExample = new MenuExample();
        menuExample.createCriteria().andIdEqualTo(menu.getId())
                .andParentIdEqualTo(menu.getParentId())
                .andNameEqualTo(menu.getName());
        List<Menu> menus = menuMapper.selectByExample(menuExample);
        Menu menu1 = null;
        if (menus != null && menus.size() == 1) {
            menu1 = menus.get(0);
        }
        return menu1;
    }

    /**
     * 修改菜单
     * @param menuForm 菜单表单
     * @return 更新结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateMenu(MenuForm menuForm){
        Menu menu = new Menu();
        BeanUtil.copyProperties(menuForm,menu);
        return menuMapper.updateByPrimaryKeySelective(menu) > 0;
    }

    /**
     * 删除菜单
     * @param id 菜单ID
     * @return 删除结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteMenu(Long id) {
        return menuMapper.deleteByPrimaryKey(id) > 0;
    }

}
